package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    /**
     * 根据信息查询出一串员工信息
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Emp> selectPage(String name,
    Integer gender, LocalDate begin, LocalDate end,
    Integer page, Integer pageSize) {
        //1.员工分页信息
 /*       int index = (page - 1) * pageSize;

        List<Emp> empList = empMapper.selectPage(name,gender,begin,end,page,pageSize);


         Long count = empMapper.selectCount(name,gender,begin,end,page,pageSize);

         PageBean<Emp> empPageBean = new PageBean<>();

         empPageBean.setRows(empList);
         empPageBean.setTotal(count);
*/
        PageHelper.startPage(page,pageSize);
        //1.员工信息分页
        Page<Emp> empPage = empMapper.selectList(name,gender,begin,end);

        //2.新建
        PageBean<Emp> empPageBean = new PageBean<>();

        empPageBean.setTotal(empPage.getTotal());
        empPageBean.setRows(empPage.getResult());

        return empPageBean;
    }

    /**
     * 删除员工信息
     * @param ids
     */
    @Override
    public void deletelist(List<Integer> ids) {
      empMapper.deletelist( ids);
    }

    /**
     * 根据学生id,查询一个学生
     * @param id
     * @return
     */
    @Override
    public Emp selectOne(Integer id) {
        Emp emp = empMapper.selectOne(id);
        emp.setPassword(null);
        return emp;
    }

    /**
     * 添加一个学生
     * @param emp
     */
    @Override
    public void insertOne(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //设置默认密码
        emp.setPassword("123456");
        empMapper.insertOne(emp);
    }


    /**
     * 学生登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public Emp login(String username, String password) {
        Emp emp = empMapper.loginByUsernameAndPassword(username,password);

        return emp;
    }

    @Override
    public void updateOne(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.updateOne(emp);
    }
}
